package com.example.book.test;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class SimpleRabbitMQProducer {

    private final RabbitTemplate rabbitTemplate;

    public SimpleRabbitMQProducer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    // 服务启动后自动发送一条测试消息
    @PostConstruct
    public void sendTestMessage() {
        rabbitTemplate.convertAndSend("test.queue", "Hello, RabbitMQ!"); // 直接发送到队列（绕过交换机）
        System.out.println("已发送测试消息到队列: test.queue");
    }
}